package info.bond.rp.experiments;


public class CombinationsGenerator {
	
	private MaxValueIndexPair[] indexes;

	public CombinationsGenerator(Integer[] maxCountOfValuesArray) {
		int length = maxCountOfValuesArray.length;
		this.indexes = new MaxValueIndexPair[length];
		
		for (int i = 0; i < length; i++) {
			Integer maxCountOfValue = maxCountOfValuesArray[i];
			MaxValueIndexPair maxValueIndexPair = new MaxValueIndexPair(maxCountOfValue);
			indexes[i] = maxValueIndexPair;
		}
	}
	
	public Integer[] getCombination() {
		int length = indexes.length;
		Integer[] combinationIndexes = new Integer[length];
		for (int i = 0; i < length; i++) {
			MaxValueIndexPair maxValueIndexPair = indexes[i];
			Integer valueIndex = maxValueIndexPair.getValueIndex();
			combinationIndexes[i] = valueIndex;
		}
		
		return combinationIndexes;
	}
	
	public void generateNextCombination() {
		int length = indexes.length;
		int column = 0;
		while (column < length) {
			MaxValueIndexPair maxValueIndexPair = indexes[column];
			if (!maxValueIndexPair.isLastIndex()) {
				maxValueIndexPair.increaseValueIndex();
				break;
			} else {
				maxValueIndexPair.resetValueIndex();
			}
			column++;
		}
	}
	
	/**
	 * Returns true when last combination is remaining 
	 * 
	 * @return
	 */
	public Boolean isLast() {
		Boolean isLast = Boolean.FALSE;
		
		int length = indexes.length;
		MaxValueIndexPair lastValueIndexPair = indexes[length-1];
		Integer valueIndex = lastValueIndexPair.getValueIndex();
		Integer maxValueIndex = lastValueIndexPair.getMaxValueIndex();
		if (valueIndex.equals(maxValueIndex)) {
			isLast = Boolean.TRUE;
		}
		return isLast;
	}
}